﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Daybreak.Core;
using Daybreak.Core.Rendering;
using Daybreak.Core.SceneGraph;
using Daybreak.Core.Terrains;
using Daybreak.Core.Rendering.Materials;
using Daybreak.Core.Rendering.Pipeline;


namespace Tutorial3
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Demo : Tutorials.TutorialApp
    {
        public Demo()
        {
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            base.LoadContent();

            scene.SceneInstance.CreateDefaultLighting();

            Group root = new Group();

            scene.SceneInstance.AddRootNode(root);

            CreateScene(root, scene);

            scene.Images.Load(false);

            xnaEngine.ShowScene(scene);

            xnaEngine.Camera = new MouseCamera(1);
            xnaEngine.Camera.Position = new Vector3(20, 10, 20);
            xnaEngine.Camera.RotationDegrees = new Vector2(10, 230);

        }


        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void DrawFrame(GameTime gameTime)
        {
            int time = (int)gameTime.ElapsedRealTime.TotalMilliseconds;
            WriteText("FPS " + fpsCounter.Fps.ToString(), 10, 10, Color.Yellow);
            WriteText("FrameTime (ms) x 60 = " + time * 60, 10, 25, Color.Yellow);

        }


        private void CreateScene(Group rootNode, Scene scene)
        {
            Scene.Image img1 = scene.CreateImage("texture1", "default_color.dds");
            Scene.Image img2 = scene.CreateImage("texture2", "default_bump_normal.dds");
            img1.Load(scene.Images);

            Scene.Material mat1 = scene.CreateMaterial("default1", img1);
            Scene.Material mat2 = Scene.Material.Debug;

            FxMaterial mat3 = FxMaterial.Compile("velvety.fx");
            FxMaterial mat4 = FxMaterial.Compile("glossyHilight.fx");
            FxMaterial mat5 = FxMaterial.Compile("DiffuseBump.fx");

            mat3.SetTexture(SasSemantic.diffuse, img1.Texture, 0);
            mat4.SetTexture(SasSemantic.diffuse, img1.Texture, 0);
            mat5.SetTexture(SasSemantic.diffuse, img1.Texture, 0);
            mat5.SetTexture(SasSemantic.normal, img2.Texture, 0);

            // create textured sphere geometry
            Geometry sphereGeom = scene.CreateGeometry("Sphere01-mesh");
            Geometry.Factory.CreateSphereMesh(1.5f, 15, 17, sphereGeom, mat4);

            // create textured box geometry
            Geometry boxGeom = scene.CreateGeometry("Box01-mesh");
            Geometry.Factory.CreateBoxMesh(1, 1, 1, boxGeom, mat3);


            Transform.Matrix44 transformNode1 = new Transform.Matrix44();
            transformNode1.Move(0, 1, 0, false);

            Geode geoNode1 = new Geode();
            Geode geoNode2 = new Geode();
            Geode geoNode3 = new Geode();

            geoNode1.AddDrawables(sphereGeom.CreateInstance(scene, mat1));
            geoNode2.AddDrawables(sphereGeom.CreateInstance(scene, mat3));
            geoNode3.AddDrawables(sphereGeom.CreateInstance(scene, mat4));

            Transform.Matrix44 transformNode2 = new Transform.Matrix44();
            transformNode2.Move(3, 0, 0, false);

            Transform.Matrix44 transformNode3 = new Transform.Matrix44();
            transformNode3.Move(-3, 0, 0, false);

            // adding the same box node three times
            transformNode1.AddChild(geoNode1); // root
            transformNode2.AddChild(geoNode2);
            transformNode3.AddChild(geoNode3);

            // create hierarchy
            transformNode1.AddChild(transformNode2);
            transformNode1.AddChild(transformNode3);

            rootNode.AddChild(transformNode1);

        }

    }
}
